package com.me.web;

import com.me.pojo.Pokemon;
import com.me.pojo.Trainer;
import com.me.service.PokemonService;
import com.me.service.TrainerService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/addtrainerServlet")
public class addtrainerServlet extends HttpServlet {
private TrainerService trainerService = new TrainerService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码
        request.setCharacterEncoding("utf-8");
        //1.接受表单数据
        String trainername = request.getParameter("trainername");
        String trainersex = request.getParameter("trainersex");
        String trainerage = request.getParameter("trainerage");
        String trainerarea = request.getParameter("trainerarea");
        //2.封装成pokemon对象
        Trainer trainer = new Trainer();
        trainer.setName(trainername);
        trainer.setSex(trainersex);
        trainer.setAge(Integer.parseInt(trainerage));
        trainer.setArea(trainerarea);
        //3.调用service保存到数据库
        trainerService.addtrainer(trainer);
        //4.转发到宝可梦全图鉴
        request.getRequestDispatcher("/trainerallServlet").forward(request,response);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
